package com.example.hello.service.impl;

import com.example.hello.dto.CategoryDTO;
import com.example.hello.entity.Category;
import com.example.hello.mapper.AttractionMapper;
import com.example.hello.mapper.CategoryMapper;
import com.example.hello.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    
    @Autowired
    private AttractionMapper attractionMapper;

    @Override
    @Transactional
    public boolean addCategory(CategoryDTO categoryDTO) {
        // 检查类别名称是否已存在
        Category existCategory = categoryMapper.getCategoryByName(categoryDTO.getCategoryName());
        if (existCategory != null) {
            throw new RuntimeException("景点类别名称已存在");
        }
        
        // 设置默认值
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        if (category.getSortOrder() == null) {
            category.setSortOrder(0);
        }
        if (category.getIsHot() == null) {
            category.setIsHot(false);
        }
        
        return categoryMapper.insertCategory(category) > 0;
    }

    @Override
    @Transactional
    public boolean updateCategory(CategoryDTO categoryDTO) {
        // 检查类别是否存在
        Category existCategory = categoryMapper.getCategoryById(categoryDTO.getCategoryId());
        if (existCategory == null) {
            throw new RuntimeException("景点类别不存在");
        }
        
        // 检查类别名称是否被其他类别占用
        if (categoryDTO.getCategoryName() != null && !categoryDTO.getCategoryName().equals(existCategory.getCategoryName())) {
            Category nameExistCategory = categoryMapper.getCategoryByName(categoryDTO.getCategoryName());
            if (nameExistCategory != null) {
                throw new RuntimeException("景点类别名称已存在");
            }
        }
        
        // 更新类别信息
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        
        return categoryMapper.updateCategory(category) > 0;
    }

    @Override
    @Transactional
    public boolean deleteCategory(Integer categoryId) {
        // 检查类别是否存在
        Category existCategory = categoryMapper.getCategoryById(categoryId);
        if (existCategory == null) {
            throw new RuntimeException("景点类别不存在");
        }
        
        // 检查该类别下是否有景点，如果有则不允许删除
        int attractionCount = attractionMapper.countByCategoryId(categoryId);
        if (attractionCount > 0) {
            throw new RuntimeException("该景点类别下存在" + attractionCount + "个景点，无法删除");
        }
        
        return categoryMapper.deleteCategoryById(categoryId) > 0;
    }

    @Override
    public Category getCategoryById(Integer categoryId) {
        return categoryMapper.getCategoryById(categoryId);
    }

    @Override
    public List<Category> getAllCategories() {
        return categoryMapper.getAllCategories();
    }

    @Override
    public PageInfo<Category> getCategoryList(Category category, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Category> list = categoryMapper.getCategoryList(category);
        return new PageInfo<>(list);
    }
} 